<?php

namespace App\Admin\Controllers;

use App\Models\Scene;
use App\Models\Spot;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;

class SpotController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('景区标注');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Spot::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->disableExport();
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->like('title','名称');
                $filter->equal('scene_id','景区选择')->select(Scene::where('is_docking',1)->pluck('title', 'id'));
                $filter->equal('type','类别')->select(config('options.map_point_type'));
            });
            $grid->id('ID')->sortable();
            $grid->cover('图片')->image('http://112.74.55.125:8686/storage',100,100);
            $grid->title('标题');
            $grid->latlng('坐标');
            $grid->address('地址');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Spot::class, function (Form $form) {
            $form->select('scene_id','景区')->options(Scene::all()->pluck('title','id'));
            $form->select('type', '类型')->options(config('options.map_point_type'));
            $form->text('title', '名称');
            $form->image('cover','图片')->uniqueName()->removable();
            $form->tags('latlng','坐标');
            $form->text('address','地址');
        });
    }
}
